package itsos.service.impl;

import itsos.model.*;
import itsos.service.*;
import itsos.dao.*;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.*;

import javax.servlet.http.*;

import org.springframework.web.servlet.ModelAndView;
import itsos.common.*;

public class MainIssueServiceImpl implements MainIssueService {

	private MainIssueDao mainIssueDao;
	private IssueDao issueDao;
	
	
	public void setIssueDao(IssueDao issueDao)
	{
		this.issueDao = issueDao;
	}
	
	public void setMainIssueDao(MainIssueDao mainIssueDao)
	{
		this.mainIssueDao = mainIssueDao;
	}
	
	
	
	public void insertMainIssue(HttpServletRequest request) {

		MainIssue mainIssue = new MainIssue();
		mainIssue.setRepositoryID(new Long(Long.parseLong(request.getParameter("repositoryID"))));
		mainIssue.setRepositoryName(request.getParameter("repositoryName"));
		mainIssue.setBeforeMainIssueID(new Long(Long.parseLong(request.getParameter("beforeRepositoryID"))));
		mainIssue.setBeforeMainIssueName(request.getParameter("beforeMainIssueName"));
		mainIssue.setMainIssueContent(request.getParameter("mainIssueContent"));
		mainIssue.setAdminID(ConstCode.SUPER_ADMIN_NUMBER);
		mainIssue.setAdminContent(request.getParameter("adminContent"));
		mainIssue.setReleaseInfo(request.getParameter("releaseInfo"));
		mainIssue.setPublishGroupCode(new Long(Long.parseLong(request.getParameter("publishGroupCode"))));
		mainIssue.setStateCode(new Long(Long.parseLong(request.getParameter("stateCode"))));
		mainIssue.setInsertTime(new Timestamp(System.currentTimeMillis()));
		mainIssue.setInsertUserID(mainIssue.getAdminID());
		mainIssue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		mainIssue.setUpdateUserID(mainIssue.getAdminID());
		
		mainIssueDao.insertMainIssue(mainIssue);
		
	}

	public void updateMainIssue(HttpServletRequest request, HttpSession session) {

		MainIssue mainIssue = new MainIssue();
		
		try {
			ConvertUtil.populate(request, mainIssue);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mainIssueDao.updateMainIssue(mainIssue);
		
		
	}

	public MainIssue viewMainIssue(HttpServletRequest request) {
		
		MainIssue mainIssue = new MainIssue();
		long a = new Long(Long.parseLong(request.getParameter("repositoryID")));
		
		if(a!=0)
		{
			mainIssue.setRepositoryID(a);
		}
		else
		{
			long b = (Long)request.getAttribute("repositoryID");
			mainIssue.setRepositoryID(b);
		}
		
		mainIssue = mainIssueDao.viewMainIssue(mainIssue.getRepositoryID());
				
		return mainIssue;
	}

	public MainIssue getMainIssue(HttpServletRequest request) {
				
		return mainIssueDao.getMainIssue(new Long(Long.parseLong(request.getParameter("repositoryID"))));
	}

	public List selectAdmin() {
		
		List list;
		
		list = mainIssueDao.selectAdmin();
		
		return list;
	}
	
	public void adminUpdate(HttpServletRequest request)
	{
		MainIssue mainIssue = new MainIssue();
		mainIssue.setAdminID(new Long(Long.parseLong(request.getParameter("selectedAdmin"))));
		mainIssue.setMainIssueID(new Long(Long.parseLong(request.getParameter("selectedMainIssue"))));
		
		mainIssueDao.adminUpdate(mainIssue);
	}

	public MainIssue getAdminMainIssue(HttpServletRequest request) {

		MainIssue mainIssue = new MainIssue();
		mainIssue.setMainIssueID(new Long(Long.parseLong(request.getParameter("mainIssueID"))));
		mainIssue = mainIssueDao.getAdminMainIssue(mainIssue);
		
		
		return mainIssue;
	}

	public void contentUpdate(HttpServletRequest request) {
		MainIssue mainIssue = new MainIssue();
		mainIssue.setAdminContent(request.getParameter("adminContent"));
		mainIssue.setReleaseInfo(request.getParameter("releaseInfo"));
		
		mainIssueDao.adminUpdate(mainIssue);
		
	}



	
	
}
